package cn.e4j.modular.api.service;


import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

@Service
public class CommService{

    @Autowired
    E4jProperties e4jProperties;

    public <T> T exe(Class clazz, String methodName, Object... params)throws NoSuchMethodException{
        Method method = null;
        T result = null;

        Class<Object>[] parameterType = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            parameterType[i] = (Class<Object>) (params[i]).getClass();
        }
        Object tClass = SpringContextHolder.getBean(clazz.getName());
        switch (e4jProperties.getDbType()){
            case "oracle":
                    method = tClass.getClass().getMethod(methodName+"Ora", parameterType);
                    break;
            case "sqlserver":
                    method = tClass.getClass().getMethod(methodName, parameterType);
        }
        try {
           result = (T)method.invoke(clazz,params);
        }catch (IllegalAccessException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }

        return result;
    }
}
